package org.nidy.erp.interceptor;

import org.nidy.erp.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
//@Component
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //  1、判断是否为登录请求, 如果是放行
//        String uri = request.getRequestURI();
//        if (uri.contains("/login")) {
//
//            return true;
//        }
        // 2、获取请求头中的token
        String token = request.getHeader("token");
        // 3、判断token是否为空
        if (token == null || token.isEmpty()) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return false;
        }
        // 4、判断token是否有效
        try {
            JwtUtils.parseToken(token);

        } catch (Exception e) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return false;
        }
        // 5、放行
        return true;

    }
}
